﻿<UserControl x:Class="Dispatcher.Views.PlaceTimetableView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:Converters="clr-namespace:Dispatcher.Converters"
             xmlns:TemplateSelectors="clr-namespace:Dispatcher.TemplateSelectors"
             Height="500" Width="800" DataContextChanged="UserControl_DataContextChanged">
    <UserControl.Resources>

        <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#FFF" Offset="0.0"/>
                    <GradientStop Color="LightPink" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="NormalBrush1" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#FFF" Offset="0.0"/>
                    <GradientStop Color="LightGray" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="NormalBrush2" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#FFF" Offset="0.0"/>
                    <GradientStop Color="LimeGreen" Offset="1.0"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>

        <Converters:TimetableConverter x:Key="TimetableConverter"/>
        <Converters:EventConverter x:Key="EventConverter"/>
        <Converters:EventTooltipConverter x:Key="EventTooltipConverter"/>
        <Converters:EventButtonConverter x:Key="EventButtonConverter"/>
        <TemplateSelectors:EventTemplateSelector x:Key="EventTemplateSelector"/>
        
        <ControlTemplate  x:Key="EventInfo">
            <GroupBox Header="Событие">
                <StackPanel  Width="180">
                    <Button x:Name="ReserveButton" Click="ReserveButton_Click">
                        <TextBlock  Text="{Binding Converter={StaticResource EventButtonConverter}}"/>
                    </Button>
                    <TextBlock 
                        Name="EventDescription" 
                        Text="{Binding Converter={StaticResource EventTooltipConverter}}"/>
                </StackPanel>
            </GroupBox>
        </ControlTemplate>

        <DataTemplate x:Key="StandartTemplate">

            <Button IsEnabled="True"
                BorderBrush="LightGray"
                BorderThickness="0"
                Background="{StaticResource NormalBrush1}"
                Height="{Binding Converter={StaticResource TimetableConverter}}">
                <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding StartTime, StringFormat=HH:mm}"/>
                <TextBlock Text="-"/>
                <TextBlock Text="{Binding EndTime, StringFormat=HH:mm}"/>
                </StackPanel>
            </Button>

        </DataTemplate>
        
        <ControlTemplate x:Key="BusyButtonTemplate">

            <Button Width="150"
                    Click="ChangeEvent"
                    BorderBrush="LightPink"
                    BorderThickness="0"
                    PreviewMouseDown="FocusButton"
                    Background="{StaticResource NormalBrush}"
                    ToolTip="{Binding Converter={StaticResource EventTooltipConverter}}">
                <TextBlock Text="{Binding Converter={StaticResource EventConverter}}"/>
            </Button>

        </ControlTemplate>

        <ControlTemplate x:Key="PeriodicEventButtonTemplate">

            <Button Width="150"
                    Click="ChangeEvent"
                    BorderBrush="LimeGreen"
                    BorderThickness="0"
                    PreviewMouseDown="FocusButton"
                    Background="{StaticResource NormalBrush2}"
                    ToolTip="{Binding Converter={StaticResource EventTooltipConverter}}">
                <TextBlock Text="{Binding Converter={StaticResource EventConverter}}"/>
            </Button>

        </ControlTemplate>
        <ControlTemplate x:Key="FreeButtonTemplate">
            <Button Width="150"
                Background="White" 
                    BorderBrush="LightBlue"
                    BorderThickness="0" 
                    Click="ChangeEvent" 
                    PreviewMouseDown="FocusButton"
                    MouseDoubleClick="ReserveButton_MouseDoubleClick">
                <TextBlock Background="White"/>
            </Button>
        </ControlTemplate>

        <DataTemplate x:Key="PeriodicEventTemplate">
            <ContentControl
                Template="{StaticResource PeriodicEventButtonTemplate}"   
                Height="{Binding Converter={StaticResource TimetableConverter}}">
                <ContentControl.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Освободить событие" Click="ReserveButton_Click"/>
                    </ContextMenu>
                </ContentControl.ContextMenu>
            </ContentControl>
        </DataTemplate>
        
        <DataTemplate x:Key="BusyEventTemplate">
            <ContentControl
                Template="{StaticResource BusyButtonTemplate}"   
                Height="{Binding Converter={StaticResource TimetableConverter}}">
                <ContentControl.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Освободить событие" Click="ReserveButton_Click"/>
                    </ContextMenu>
                </ContentControl.ContextMenu>
            </ContentControl>
        </DataTemplate>

        <DataTemplate x:Key="FreeEventTemplate">
            <ContentControl 
                Template="{StaticResource FreeButtonTemplate}"                                   
                ToolTip="{Binding Converter={StaticResource EventTooltipConverter}}"
                Height="{Binding Converter={StaticResource TimetableConverter}}">
                <ContentControl.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Создать" Click="ReserveButton_Click"/>
                    </ContextMenu>
                </ContentControl.ContextMenu>
            </ContentControl>
        </DataTemplate>

        <DataTemplate x:Key="ColumnTemplate">
            <ItemsControl BorderBrush="White" 
                ItemsSource="{Binding}" 
                ItemTemplateSelector="{StaticResource EventTemplateSelector}"/>
        </DataTemplate>

        <DataTemplate x:Key="PairColumnTemplate">
            <Grid Background="WhiteSmoke">
            
            <ItemsControl BorderBrush="White" ItemsSource="{Binding}" ItemTemplate="{StaticResource StandartTemplate}"/>
        </Grid>
        </DataTemplate>

        <DataTemplate x:Key="DateColumnTemplate">
            <Button Width="100" Background="WhiteSmoke" IsEnabled="True">
                <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                    <TextBlock Text="{Binding Date, StringFormat=dddd}"/>
                    <TextBlock Text="{Binding Date, StringFormat=d}"/>
                </StackPanel>
            </Button>
        </DataTemplate>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger Property="DataGridCell.IsSelected" Value="True">
                    <Setter Property="Background" Value="LightSteelBlue" />
                    <Setter Property="BorderBrush" Value="{StaticResource NormalBrush1}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>          
        </Grid.ColumnDefinitions>
        
        <DataGrid ScrollViewer.ScrollChanged="DataGrid_ScrollChanged" Name="DataGrid" />
        <StackPanel Orientation="Vertical" Grid.Column="1">
            <GroupBox Grid.Column="1" Header="Пользователь">
                <StackPanel>
                    <TextBlock Margin="5" Name="UserName" Visibility="Collapsed"/>
                    <Button Name="LoginButton" Click="LoginButton_Click">Вход</Button>
                    <Button Name="AdminToolsButton" Click="AdminToolsButton_Click">Администрирование</Button>
                </StackPanel>
            </GroupBox>
            <GroupBox Grid.Column="1" Header="Недели">
                <StackPanel>
                    <Button Click="PrevWeekButton_Click">Предыдущая</Button>
                    <Button Click="NextWeekButton_Click">Следующая</Button>
                    <Calendar Height="170" Name="Calendar1" Width="180" SelectedDatesChanged="Calendar1_SelectedDatesChanged" />
                </StackPanel>
            </GroupBox>
        <ContentControl  Name="EventInfo" Template="{StaticResource EventInfo}"/>
        </StackPanel>
    </Grid>
</UserControl>
